<?php

// Include the different user types so we can filter out unauthorized users
include("include/dbUserTypes.php");

// Include our user class
include_once("include/user.php");

// Create a variable to store our user if we're validated
$user = null;

// Check if we're in a valid session. If not, go back to main login page.
if( !isset($_SESSION['user']) || $_SESSION['user'] == null ) {
    header("location:main_login.php");
}
else {
    // Unserialize our user to turn it back into a useful object
    $user = unserialize($_SESSION['user']);
}

// If we're not an admin go back to the user redirection page
if( $user->getUserType() != $USERTYPE_ADMIN ) {
    header("location:login_success.php");
}
else
{
    // Connect to our database
    include("include/dbconnection.php");
    
    // Select all homes
    $db_query = "SELECT 
    $TABLE_HOME_HOMEID, $TABLE_HOME_ADDRESS, $TABLE_HOME_STREETNAME, $TABLE_HOME_CITY,
    $TABLE_HOME_COUNTRY, $TABLE_HOME_POSTALCODE
    FROM $TABLE_HOME
    WHERE $TABLE_HOME_SOLDTO = 0";
    
    // Get the results of the query
    $resultSet = mysql_query($db_query);

    // If we didn't get results, output simple message.
    // Otherwise show the new users.
    if(mysql_num_rows($resultSet) == 0)
        echo "No homes exist to manage.";
    else
    {?>     
        <table style="width: 740px">
            <tr style="background: #FFFFFF; color: #74653D;">
                <th>Home ID</th>
                <th>Address</th>
                <th>City</th>
                <th>Country</th>
                <th>Postal Code</th>
                <th>Edit</th>
                <th>Delete</th>
            </tr>
        <?php
        // Show all the homes and add edit and delete links.
        while( $row = mysql_fetch_array($resultSet) ) {
            ?>
            <tr>
                <td><?php echo $row[$TABLE_HOME_HOMEID]?></td>
                <td><?php echo $row[$TABLE_HOME_ADDRESS]." ".$row[$TABLE_HOME_STREETNAME]?></td>
                <td><?php echo $row[$TABLE_HOME_CITY]?></td>
                <td><?php echo $row[$TABLE_HOME_COUNTRY]?></td>
                <td><?php echo $row[$TABLE_HOME_POSTALCODE]?></td>
                <td><a href="admin_manageListings.php?id=<?php echo $row[$TABLE_HOME_HOMEID]?>">Edit</a></td>
                <td style="padding-top: 11px">
                    <form name="deleteForm_<?php echo $row[$TABLE_HOME_HOMEID]?>" method="post" action="include/admin/deleteHome.php">
                        <input name="deleteID" type="hidden" value="<?php echo sha1($row[$TABLE_HOME_STREETNAME].$row[$TABLE_HOME_HOMEID])?>"/>
                        <input name="id" type="hidden" value="<?php echo $row[$TABLE_HOME_HOMEID]?>"/>
                        <input name="streetName" type="hidden" value="<?php echo $row[$TABLE_HOME_STREETNAME]?>"/>
                        <a href="javascript: if(confirm('Are you sure you want to delete the home at <?php echo $row[$TABLE_HOME_ADDRESS]." ".$row[$TABLE_HOME_STREETNAME]?>?') )deleteForm_<?php echo $row[$TABLE_HOME_HOMEID]?>.submit();">Delete</a>
                    </form>                    
                </td>
            </tr>          
            <?php
        } // End while loop ?>
        </table>
    <?php
    } // End else statement
    
    // Close the connection
    mysql_close();
    
    
    
}


